﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Account : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bindata();
    }
    public void Bindata()
    {
        AcountBOL aBOL = new AcountBOL();
        GridView1.DataSource = aBOL.LoadAcount();
        GridView1.DataBind();
    }
    protected void UpdateRecord(object s, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        TextBox tusername = (TextBox)row.FindControl("txtusername");
        TextBox tpass = (TextBox)row.FindControl("txtpass");
        TextBox tIdentification = (TextBox)row.FindControl("txtIdentification");
        TextBox tdate_Registry = (TextBox)row.FindControl("txtdate_Registry");
        TextBox tAcountType = (TextBox)row.FindControl("txtAcountType");

        Acount a = new Acount();
        AcountBOL aBOL = new AcountBOL();
        try
        {
            a.Username = tusername.Text;
            a.Pass = tpass.Text;
            a.Identification = tIdentification.Text;
            a.Dateregistry = Convert.ToDateTime(tdate_Registry.Text);
            a.Acounttype = tAcountType.Text;

            int result = aBOL.updateAcount(a);
            if (result > 0)
            {
                LResult.Text = "Update record is successful!";
            }
            else
                LResult.Text = "Update record is error";
        }
        catch (Exception ex)
        {
            LResult.Text = ex.Message;
        }
        finally
        {
            a = null;
            aBOL = null;
        }
        GridView1.EditIndex = -1;
        Bindata();
    }
    protected void EditRecord(object s, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bindata();
    }
    protected void CancelRecord(object s, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bindata();
    }
    protected void DeleteRecord(object s, GridViewDeleteEventArgs e)
    {
        string tusername = GridView1.DataKeys[e.RowIndex].Value.ToString();

        Acount a = new Acount();
        AcountBOL aBOL = new AcountBOL();
        try
        {    
            a.Username = tusername;
            int result = aBOL.deleteAcount(a);
            if (result > 0)
                LResult.Text = "Delete record is successful.";
            else
                LResult.Text = "Delete record is error.";
        }
        catch (Exception ex)
        {
            LResult.Text = ex.Message;
        }
        finally
        {

            a = null;
            aBOL = null;
        }
            GridView1.EditIndex = -1;
            Bindata();
    }
}